// pages/order/order.js
// 导入获取数据的方法
import { request } from '../../request/index.js'
import regeneratorRuntime, { async } from '../../lib/runtime/runtime';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        tabs: [
            {
                id: 0,
                value: '全部',
                isActive: true
            },
            {
                id: 1,
                value: '待付款',
                isActive: false
            },
            {
                id: 2,
                value: '待收货',
                isActive: false
            },
            {
                id: 3,
                value: '退款/退货',
                isActive: false
            }
        ],
        orders: [
            {
                orders_id: 10001,
                orders_number: 'xc215634231',
                orders_price: 5999,
                orders_time: "2021/10/26 21:42:20"
            },
            {
                orders_id: 10002,
                orders_number: 'xc215634232',
                orders_price: 6999,
                orders_time: "2021/10/26 21:42:20"
            },
            {
                orders_id: 10003,
                orders_number: 'xc215634233',
                orders_price: 7999,
                orders_time: "2021/10/26 21:42:20"
            },
            {
                orders_id: 10004,
                orders_number: 'xc215634234',
                orders_price: 8999,
                orders_time: "2021/10/26 21:42:20"
            },
        ]
    },
    onShow(options) {
        // 判断缓存中是否有token(暂时没有token)
        // const token = wx.getStorageSync('token')
        // if (!token) {
        //     wx.navigateTo({
        //         url: '/pages/auth/auth',
        //     })
        //     return
        // }
        // 获取小程序当前的页面栈-数组 长度最大为10
        var pages = getCurrentPages();
        // 数组中，索引最大的就是当前页面
        let currentPages = pages[pages.length - 1]
        // 拿到type
        const { type } = currentPages.options
        this.changeTitleByIndex(type - 1)
        this.getOrders(type)
    },
    // 获取订单
    async getOrders(type) {
        const res = await request({ url: '/my/orders/all', data: { type } })
        // this.setData({
        //     orders: res.orders
        // })
    },
    // 根据标题索引来选中标题数组
    changeTitleByIndex(index) {
        let { tabs } = this.data
        tabs.forEach((v, i) => i === index ? v.isActive = true : v.isActive = false)
        this.setData({
            tabs
        })
    },
    // 点击标题切换事件
    bindTabsItemChange(e) {
        const { index } = e.detail
        this.changeTitleByIndex(index)
        //  当手动切换了标题时，重新发送请求(type=index+1)
        this.getOrders(index + 1)
    }
})